﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WenSkin.Controls
{
    public class WenTreeViewX : WenTreeView
    {
        public WenTreeViewX() : base()
        {
            base.SetStyle(
                ControlStyles.DoubleBuffer |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.ResizeRedraw |
                ControlStyles.SupportsTransparentBackColor, true);
            base.UpdateStyles();
            this.DrawMode = TreeViewDrawMode.OwnerDrawAll;
            this.FullRowSelect = true;
            this.ItemHeight = 30;
            this.BackColor = Color.FromArgb(37, 37, 38);
            this.ForeColor = Color.White;
        }

        #region 私有属性

        public StringFormat stringConters = new StringFormat(StringFormatFlags.NoClip | StringFormatFlags.NoWrap) { LineAlignment = StringAlignment.Center, };

        #endregion

        #region 公有属性

        [Category("Wen"), Description("关闭时候图标")]
        [DefaultValue(null)]
        public Image ImageClose { get; set; } = Properties.Resources.Tright;

        [Category("Wen"), Description("打开时候图标")]
        [DefaultValue(typeof(Image),"Properties.Resources.Tdown")]
        public Image ImageOpen { get; set; } = Properties.Resources.Tdown;

        [Category("Wen"), Description("打开关闭按钮图标大小")]
        [DefaultValue(typeof(Size), "16, 16")]
        public Size ButtonImageSize { get; set; } = new Size(16, 16);

        [Category("Wen"), Description("鼠标移动的颜色")]
        [DefaultValue(typeof(Color), "62, 62, 64")]
        public Color MouseMoveColor { get; set; } = Color.FromArgb(62, 62, 64);

        [Category("Wen"), Description("选中颜色")]
        [DefaultValue(typeof(Color), "63, 63, 70")]
        public Color SelectedColor { get; set; } = Color.FromArgb(63, 63, 70);

        [Category("Wen"), Description("图标尺寸")]
        [DefaultValue(typeof(Size), "16, 16")]
        public Size ImageSize { get; set; } = new Size(16, 16);

        #endregion

        protected override void OnDrawNode(DrawTreeNodeEventArgs e)
        {
            if (e.Bounds == new Rectangle(0, 0, 0, 0))
                return;


            Graphics g = e.Graphics.SetGDIHigh();
            //Rectangle rec = new Rectangle(0, e.Bounds.Y, this.Width, ItemHeight - 1);
            Rectangle rec = e.Bounds;
            rec.Inflate(-1, -1);
            TreeNode node = e.Node;

            //画背景
            if (node.IsSelected)
            {
                g.FillRectangle(new SolidBrush(SelectedColor), rec);
            }
            else if (node == MouseTreeNode)
            {
                g.FillRectangle(new SolidBrush(MouseMoveColor), rec);
            }
            else
            {
                g.FillRectangle(new SolidBrush(node.BackColor), rec);
            }

            Console.WriteLine(e.Bounds);

            //画节点
            Rectangle recLevel = new Rectangle(rec.X, rec.Y, (node.Level + 1) * 20, rec.Height);

            //设置展开关闭图标位置
            Rectangle recbuttonImageSize = 
                new Rectangle(recLevel.X + recLevel.Width - ButtonImageSize.Width,
                recLevel.Y + (ItemHeight - ButtonImageSize.Height) / 2,
                ButtonImageSize.Width, ButtonImageSize.Height);

            if (node.Nodes.Count != 0)
            {
                if (node.IsExpanded)
                    g.DrawImage(ImageOpen, recbuttonImageSize);
                else
                    g.DrawImage(ImageClose, recbuttonImageSize);
            }


            //文字边框
            Rectangle recString = new Rectangle(rec.X + recLevel.Width, rec.Y, rec.Width , rec.Height);

            //画图标
            if (this.ImageList != null && node.ImageIndex >= 0)
            {
                //设置图标区域
                Rectangle recImage =
                    new Rectangle(new Point(rec.X + recLevel.Width,
                    recLevel.Y + (ItemHeight - ImageSize.Height) / 2),
                    ImageSize);
                g.DrawImage(ImageList.Images[node.ImageIndex], recImage);

                recString = new Rectangle(recImage.X + recImage.Width, rec.Y, rec.Width - recImage.Width - recLevel.Width, rec.Height);
            }

            //画文字
            g.DrawString(node.Text, this.Font, new SolidBrush(this.ForeColor), recString, stringConters);
        }

        //刷新节点
        private void InvalidateTreeNode(TreeNode node)
        {
            if (node == null)
                return;
            Rectangle rec = new Rectangle(0, node.Bounds.Y, this.Width, ItemHeight + 1);
            this.Invalidate(rec);
        }

        #region 鼠标移动效果

        private TreeNode MouseTreeNode = null;

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            if (MouseTreeNode != null)
                InvalidateTreeNode(MouseTreeNode);
            MouseTreeNode = null;
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            TreeNode node = this.GetNodeAt(e.Location);
            if (node != MouseTreeNode)
            {
                if (MouseTreeNode != null)
                    InvalidateTreeNode(MouseTreeNode);
                InvalidateTreeNode(node);
                MouseTreeNode = node;
            }
        }
        #endregion

        #region 鼠标点击效果

        //protected override void OnMouseClick(MouseEventArgs e)
        //{
        //    base.OnMouseClick(e);
        //    TreeNode node = this.GetNodeAt(e.Location);
        //    this.SelectedNode = node;
        //}

        #endregion

        #region 鼠标双击

        //protected override void OnMouseDoubleClick(MouseEventArgs e)
        //{
        //    base.OnMouseDoubleClick(e);
        //    TreeNode node = this.GetNodeAt(e.Location);
        //    if (node.Nodes.Count == 0 || node.Bounds.Contains(e.Location))
        //        return;

        //    //if (node.IsExpanded)
        //    //    node.Expand();
        //    //else
        //        node.Toggle();
        //}

        #endregion
    }
}
